# Copyright 2019 The IREE Authors
#
# Licensed under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

load("//build_tools/bazel:build_defs.oss.bzl", "iree_cmake_extra_content", "iree_compiler_cc_library", "iree_gentbl_cc_library")

package(
    default_visibility = ["//visibility:public"],
    features = ["layering_check"],
    licenses = ["notice"],  # Apache 2.0
)

iree_gentbl_cc_library(
    name = "PassesIncGen",
    tbl_outs = [
        (
            ["--gen-pass-decls"],
            "Passes.h.inc",
        ),
    ],
    tblgen = "@llvm-project//mlir:mlir-tblgen",
    td_file = "Passes.td",
    deps = [
        "@llvm-project//mlir:PassBaseTdFiles",
    ],
)

iree_compiler_cc_library(
    name = "Transforms",
    srcs = [
        "AnnotateDispatches.cpp",
        "CaptureDispatchDynamicDims.cpp",
        "CleanupTensorShapes.cpp",
        "CloneProducersIntoDispatchRegions.cpp",
        "CollapseDimensions.cpp",
        "CollapseReductionDims.cpp",
        "ConvertRegionToWorkgroups.cpp",
        "ConvertToFlow.cpp",
        "DeduplicateExecutables.cpp",
        "DispatchWithTransformDialect.cpp",
        "DumpDispatchGraph.cpp",
        "ExportBenchmarkFuncs.cpp",
        "FoldUnitExtentDims.cpp",
        "FormDispatchRegions.cpp",
        "FormDispatchWorkgroups.cpp",
        "FormScalarDispatches.cpp",
        "FusionOfTensorOps.cpp",
        "InitializeEmptyTensors.cpp",
        "InjectDispatchTracing.cpp",
        "InsertDispatchDebugTargets.cpp",
        "InterchangeGenericOps.cpp",
        "InterchangeTransposeGenericOps.cpp",
        "OutlineDispatchExterns.cpp",
        "OutlineDispatchRegions.cpp",
        "PassDetail.h",
        "Passes.cpp",
        "RegionOpUtils.cpp",
        "SplitReduction.cpp",
        "TensorPadToTensorInsertSlice.cpp",
        "TopLevelSCFToCFG.cpp",
        "VerifyInputLegality.cpp",
    ],
    hdrs = [
        "ConvertRegionToWorkgroups.h",
        "FormDispatchRegions.h",
        "Passes.h",
        "Passes.h.inc",
        "RegionOpUtils.h",
    ],
    deps = [
        ":PassesIncGen",
        "//compiler/src/iree/compiler/Dialect/Flow/Conversion/TensorToFlow",
        "//compiler/src/iree/compiler/Dialect/Flow/IR",
        "//compiler/src/iree/compiler/Dialect/HAL/IR",
        "//compiler/src/iree/compiler/Dialect/Util/Analysis",
        "//compiler/src/iree/compiler/Dialect/Util/IR",
        "//compiler/src/iree/compiler/Dialect/Util/Transforms",
        "//compiler/src/iree/compiler/Utils",
        "//llvm-external-projects/iree-dialects:IREEDialectsTransforms",
        "//llvm-external-projects/iree-dialects:IREELinalgExtDialect",
        "//llvm-external-projects/iree-dialects:IREELinalgExtPasses",
        "//llvm-external-projects/iree-dialects:IREELinalgExtTransformOps",
        "//llvm-external-projects/iree-dialects:IREELinalgExtTransforms",
        "//llvm-external-projects/iree-dialects:IREELinalgExtUtils",
        "//llvm-external-projects/iree-dialects:IREELinalgTransformDialect",
        "@llvm-project//llvm:Support",
        "@llvm-project//mlir:AffineDialect",
        "@llvm-project//mlir:AffineUtils",
        "@llvm-project//mlir:Analysis",
        "@llvm-project//mlir:ArithDialect",
        "@llvm-project//mlir:ArithUtils",
        "@llvm-project//mlir:ComplexDialect",
        "@llvm-project//mlir:DialectUtils",
        "@llvm-project//mlir:FuncDialect",
        "@llvm-project//mlir:FunctionInterfaces",
        "@llvm-project//mlir:IR",
        "@llvm-project//mlir:LinalgDialect",
        "@llvm-project//mlir:LinalgTransforms",
        "@llvm-project//mlir:LinalgUtils",
        "@llvm-project//mlir:MathDialect",
        "@llvm-project//mlir:MemRefDialect",
        "@llvm-project//mlir:MemRefTransforms",
        "@llvm-project//mlir:PDLDialect",
        "@llvm-project//mlir:PDLInterpDialect",
        "@llvm-project//mlir:Parser",
        "@llvm-project//mlir:Pass",
        "@llvm-project//mlir:SCFDialect",
        "@llvm-project//mlir:SCFToControlFlow",
        "@llvm-project//mlir:Support",
        "@llvm-project//mlir:TensorDialect",
        "@llvm-project//mlir:TensorTransforms",
        "@llvm-project//mlir:TensorUtils",
        "@llvm-project//mlir:TilingInterface",
        "@llvm-project//mlir:TosaDialect",
        "@llvm-project//mlir:TransformDialect",
        "@llvm-project//mlir:TransformDialectTransforms",
        "@llvm-project//mlir:TransformUtils",
        "@llvm-project//mlir:Transforms",
    ],
)

# TODO(#4919): For an unknown reason, GCC's devirtualization optimization wreaks
# havoc on this file. Needs to be further root caused. Seems to affect both 9.x
# and 10.x.
iree_cmake_extra_content(
    content = """
set_property(SOURCE
  FormDispatchRegions.cpp
  FormDispatchWorkgroups.cpp
  PROPERTY COMPILE_FLAGS $<$<CXX_COMPILER_ID:GNU>:-fno-devirtualize>)
""",
    inline = True,
)
